package region

import (
	"encoding/binary"
	"errors"
	"strconv"
)

type Region struct {
	R1 uint8
	R2 uint8
	R3 uint8
	R4 uint8
	R5 uint16
}
type RegionTimeZone struct {
	*Region
	TimeZone string
}

func (r *Region) String() string {
	b := make([]byte, 8)
	binary.BigEndian.PutUint16(b[6:8], r.R5)
	b[5] = r.R4
	b[4] = r.R3
	b[3] = r.R2
	b[2] = r.R1
	return strconv.FormatUint(binary.BigEndian.Uint64(b), 16)
}

func Parse(s string) (*Region, error) {
	if len(s) > 12 {
		return nil, errors.New("format error")
	}
	n, err := strconv.ParseUint(s, 16, 64)
	if err != nil {
		return nil, err
	}
	b := make([]byte, 8)
	binary.BigEndian.PutUint64(b, n)
	return &Region{
		R1: b[2],
		R2: b[3],
		R3: b[4],
		R4: b[5],
		R5: binary.BigEndian.Uint16(b[6:8]),
	}, nil
}
